﻿
    using Chain.BLL;
    using Chain.Model;
  //  using ChainStock.Controls;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public partial class MemList : PageBase
    {

        public bool _needBind = true;
        public void BindCustomSelect(HtmlSelect select, string strMemCustom, string strCustomField, int intShopID)
        {
            StringBuilder sbHtml = new StringBuilder();
            if (select.Value != "")
            {
                select.Items.Clear();
                PubFunction.BindCustomField(intShopID, select, true, 1);
                select.Value = strMemCustom;
            }
            if (strCustomField != "")
            {
                string strCustomType = select.Items.FindByValue(select.Value).Attributes["CustomFieldType"].ToString();
                string strCustomInfo = select.Items.FindByValue(select.Value).Attributes["CustomFieldInfo"].ToString();
                string name1 = strCustomType;
                if (name1 != null)
                {
                    if (!(name1 == "text"))
                    {
                        if (name1 == "select")
                        {
                            string[] strInfo = strCustomInfo.Split("|".ToCharArray());
                            sbHtml.Append("<select id='txtCustomField' name ='txtCustomField' class='selectWidth' >");
                            sbHtml.Append("<option value='无'>===== 请选择 =====</option>");
                            for (int i = 0; i < strInfo.Length; i++)
                            {
                                if (strInfo[i] == strCustomField)
                                {
                                    sbHtml.Append("<option value='" + strInfo[i] + "' selected>" + strInfo[i] + "</option>");
                                }
                                else
                                {
                                    sbHtml.Append("<option value='" + strInfo[i] + "'>" + strInfo[i] + "</option>");
                                }
                            }
                            sbHtml.Append("</select>");
                        }
                        else if (name1 == "date")
                        {
                            sbHtml.Append("<input type='text' id='txtCustomField' class='Wdate border_radius' name='txtCustomField' value='" + strCustomField + "'  onfocus=\"WdatePicker({ skin: 'ext', isShowClear: false, readOnly: true });\"/>");
                        }
                    }
                    else
                    {
                        sbHtml.Append("<input type='text' id='txtCustomField' name ='txtCustomField' value='" + strCustomField + "' class='input_txt border_radius'/>");
                    }
                }
                this.ltCustom.Text = sbHtml.ToString();
            }
        }

        protected void BtnMemExcel_Click(object sender, EventArgs e)
        {
            Chain.BLL.Mem bllMember = new Chain.BLL.Mem();
            int Counts = this.NetPagerParameter.RecordCount;
            string strSql = this.QueryCondition() + "and Mem.MemShopID = SysShop.ShopID and Mem.MemLevelID = MemLevel.LevelID and Mem.MemUserID = SysUser.UserID" + " and Mem.MemShopID =SysShopMemLevel.ShopID and SysShopMemLevel.MemLevelID=MemLevel.LevelID ";
            DataTable db = bllMember.GetListSP(0x186a0, 1, out Counts, new string[] { PubFunction.GetShopAuthority(base._UserShopID, "MemShopID", strSql) }).Tables[0];
            DataExcelInfo.MemReportExcel(db, base._UserName);
        }

        protected void btnMemListQuery_Click(object sender, EventArgs e)
        {
            string strPoint = this.txtMemPoint.Value.Trim();
            string strMemMoney = this.txtMemMoney.Value.Trim();
            string strConsumeMoney = this.txtConsumeMoney.Value.Trim();
            if (strPoint != "")
            {
                try
                {
                    if (int.Parse(strPoint) < 0)
                    {
                        base.OutputWarn("会员积分必须输入大于等于0的整数！");
                        return;
                    }
                }
                catch
                {
                    base.OutputWarn("会员积分必须输入大于等于0的整数！");
                    return;
                }
            }
            if (strMemMoney != "")
            {
                try
                {
                    if (decimal.Parse(strMemMoney) < 0M)
                    {
                        base.OutputWarn("会员余额必须输入大于等于0数字");
                        return;
                    }
                }
                catch
                {
                    base.OutputWarn("会员余额必须输入大于等于0数字");
                    return;
                }
            }
            if (strConsumeMoney != "")
            {
                try
                {
                    if (decimal.Parse(strConsumeMoney) < 0M)
                    {
                        base.OutputWarn("会员余额必须输入大于等于0数字");
                        return;
                    }
                }
                catch
                {
                    base.OutputWarn("会员余额必须输入大于等于0数字");
                    return;
                }
            }
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.GetMemList(this.QueryCondition());
        }

        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
            this.GetMemList(this.QueryCondition());
        }

        protected string GetMemBirthday(DateTime birthday)
        {
            if (((birthday.Year == 0x76c) && (birthday.Month == 1)) && (birthday.Day == 1))
            {
                return "";
            }
            return birthday.ToString("yyyy-MM-dd");
        }

        private void GetMemList(string strSql)
        {
            Chain.BLL.Mem member = new Chain.BLL.Mem();
            int Counts = this.NetPagerParameter.RecordCount;
            strSql = strSql + " and Mem.MemShopID = SysShop.ShopID and Mem.MemLevelID = MemLevel.LevelID and Mem.MemUserID = SysUser.UserID ";
            strSql = strSql + " and Mem.MemShopID =SysShopMemLevel.ShopID and SysShopMemLevel.MemLevelID=MemLevel.LevelID ";
            DataTable dtMem = member.GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, new string[] { PubFunction.GetShopAuthority(base._UserShopID, "MemShopID", strSql) }).Tables[0];
            this.NetPagerParameter.RecordCount = Counts;
            this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
            this.gvMemList.DataSource = dtMem;
            this.gvMemList.DataBind();
            PageBase.BindSerialRepeater(this.gvMemList, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
            List<Chain.Model.MemCustomField> fieldlist = new Chain.BLL.MemCustomField().GetModelList("CustomType=1 and CustomFieldIsShow=1");
            if (fieldlist.Count > 0)
            {
                StringBuilder strHeader = new StringBuilder();
                StringBuilder strHtml = new StringBuilder();
                for (int i = 0; i < this.gvMemList.Items.Count; i++)
                {
                    Literal ltlMemID = (Literal) this.gvMemList.Items[i].FindControl("ltlMemID");
                    Literal ltlHtml = (Literal) this.gvMemList.Items[i].FindControl("ltlHtml");
                    int MemID = Convert.ToInt32(ltlMemID.Text);
                    DataRow[] drMem = dtMem.Select(string.Format(" MemID = {0}", MemID));
                    strHtml.Length = 0;
                    foreach (Chain.Model.MemCustomField mdCustomField in fieldlist)
                    {
                        if (i == 0)
                        {
                            strHeader.AppendFormat("<th>{0}</th>", mdCustomField.CustomFieldName);
                        }
                        strHtml.AppendFormat("<td>{0}</td>", drMem[0][mdCustomField.CustomField]);
                    }
                    ltlHtml.Text = strHtml.ToString();
                }
                this.ltlHeader.Text = strHeader.ToString();
            }
        }

        protected string GetMemState(int memState)
        {
            switch (memState)
            {
                case 0:
                    return "正常";

                case 1:
                    return "锁定";

                case 2:
                    return "挂失";
            }
            return "";
        }

        protected void gvMemList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DataRowView dr = (DataRowView) e.Item.DataItem;
                HtmlInputCheckBox chkItem = (HtmlInputCheckBox) e.Item.FindControl("chkItem");
                chkItem.Attributes.Add("memID", dr["MemID"].ToString());
                chkItem.Attributes.Add("memcard", dr["MemCard"].ToString());
                chkItem.Attributes.Add("memname", dr["MemName"].ToString());
                chkItem.Attributes.Add("mobiles", dr["MemMobile"].ToString());
                chkItem.Attributes.Add("state", dr["MemState"].ToString());
            }
        }

        protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
            this.GetMemList(this.QueryCondition());
        }
        public bool NeedBind
        {
            get
            {
                return this._needBind;
            }
            set
            {
                this._needBind = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack && this.NeedBind)
            {
                PubFunction.BindProvinceSelect(this.sltProvince);
            }
            if (!base.IsPostBack)
            {
                this.ltCustom.Text = "<input type='text' id='text' name='text' class='border_radius'/>";
                PubFunction.BindMemLevelSelect(this.sltMemLevelID, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, true);
                PubFunction.BindCustomField(base._UserShopID, this.sltCustomField, true, 1);
                PubFunction.BindUserSelect(base._UserShopID, this.sltMemUserID, true, false);
                if ((PubFunction.curParameter.dataAuthority == 1) && (base._UserShopID > 1))
                {
                    this.sltShop.Value = this._UserShopID.ToString();
                }
                this.GetMemList(this.QueryCondition());
                this.SetQuickBtn();
                this.chkSms.Checked = base.curParameter.bolSms;
                PubFunction.BindCouponSelect(this.sltCoupon, false);
                this.btnAllMemPointReset.Visible = false;
            }
        }

        protected string QueryCondition()
        {
            string name1;
            int intMemWeiXinCard = Convert.ToInt32(this.selMemWeiXinCard.Value);
            int intMemAttention = Convert.ToInt32(this.selMemAttention.Value);
            string strQueryMem = this.txtQueryMem.Value;
            string strMemLevelID = this.sltMemLevelID.Value;
            string strMemShopID = this.sltShop.Value;
            string strMemPastTime = this.sltMemPastTime.Value;
            string strMemCustom = this.sltCustomField.Value;
            string strCustomField = !string.IsNullOrEmpty(base.Request["txtCustomField"]) ? base.Request["txtCustomField"].ToString() : "";
            string strMemState = this.sltMemState.Value;
            string strMemUserID = this.sltMemUserID.Value;
            string strMemStartTime = this.txtMemStartTime.Value;
            string strMemEndTime = this.txtMemEndTime.Value;
            string strMemBirthday = this.sltMemBirthday.Value;
            string strMemConsume = this.sltMemConsume.Value;
            string strPointSymbols = this.sltMemPoint.Value;
            string strPoint = (this.txtMemPoint.Value.Trim() != "") ? this.txtMemPoint.Value.Trim() : "0";
            string strMoneySymbols = this.sltMemMoney.Value;
            string strMemMoney = (this.txtMemMoney.Value.Trim() != "") ? this.txtMemMoney.Value.Trim() : "0";
            string strConsumeMoneySymbols = this.sltConsumeMoney.Value;
            string strConsumeMoney = (this.txtConsumeMoney.Value.Trim() != "") ? this.txtConsumeMoney.Value.Trim() : "0";
            string strProvince = this.sltProvince.Value;
            string strCity = (base.Request["SysArea1$sltCity"] != null) ? base.Request["SysArea1$sltCity"].ToString() : "";
            string strCounty = (base.Request["SysArea1$sltCounty"] != null) ? base.Request["SysArea1$sltCounty"].ToString() : "";
            string strVillage = (base.Request["SysArea1$sltVillage"] != null) ? base.Request["SysArea1$sltVillage"].ToString() : "";
            string strRecommend = this.txtMemRecommendCard.Value;
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" MemID >0 ");
            switch (intMemWeiXinCard)
            {
                case 1:
                    strSql.AppendFormat("and Mem.MemWeiXinCard is not null and Mem.MemWeiXinCard!=''", new object[0]);
                    break;

                case 2:
                    strSql.AppendFormat("and (Mem.MemWeiXinCard is  null or Mem.MemWeiXinCard='')", new object[0]);
                    break;
            }
            if (intMemAttention != 0)
            {
                strSql.AppendFormat("and Mem.MemAttention={0}", intMemAttention);
            }
            if (strQueryMem != "")
            {
                strSql.AppendFormat("and (MemCard ='{0}' or MemCardNumber='{0}' or MemName like '%{0}%' or MemMobile='{0}')", strQueryMem);
            }
            if (strMemLevelID != "")
            {
                strSql.AppendFormat("and Mem.MemLevelID={0}", int.Parse(strMemLevelID));
            }
            if (strMemShopID != "")
            {
                strSql.AppendFormat("and MemShopID={0}", int.Parse(strMemShopID));
            }
            if (strMemPastTime != "")
            {
                if (strMemPastTime == "0")
                {
                    strSql.AppendFormat(" and getdate()>MemPastTime ", new object[0]);
                }
                else if (strMemPastTime == "1")
                {
                    strSql.AppendFormat(" and datediff(day,getdate(),MemPastTime)<=7 and datediff(day,getdate(),MemPastTime)>0", new object[0]);
                }
                else if (strMemPastTime == "2")
                {
                    strSql.AppendFormat(" and datediff(day,getdate(),MemPastTime)<=30 and datediff(day,getdate(),MemPastTime)>0 ", new object[0]);
                }
            }
            if (strMemConsume != "")
            {
                if (strMemConsume == "0")
                {
                    strSql.AppendFormat(" and datediff(day,MemConsumeLastTime,getdate())>91 ", new object[0]);
                }
                else if (strMemConsume == "1")
                {
                    strSql.AppendFormat(" and datediff(day,MemConsumeLastTime,getdate())>182 ", new object[0]);
                }
                else if (strMemConsume == "2")
                {
                    strSql.AppendFormat(" and datediff(day,MemConsumeLastTime,getdate())>365 ", new object[0]);
                }
            }
            if ((strMemBirthday != "") && ((name1 = strMemBirthday) != null))
            {
                if (!(name1 == "0"))
                {
                    if (name1 == "1")
                    {
                        DateTime.Now.ToShortDateString().Substring(0, 1);
                        strSql.AppendFormat(" and datediff(week,(convert(varchar(5),getdate(),120)+convert(varchar(5),MemBirthday,110)),getdate())=0 ", new object[0]);
                    }
                    else if (name1 == "2")
                    {
                        string strMonth = DateTime.Now.ToString("MM/yy").Substring(0, 2);
                        strSql.AppendFormat(" and convert(varchar(2),MemBirthday,107)={0} ", strMonth);
                    }
                }
                else
                {
                    strSql.AppendFormat(" and month(MemBirthday)=month(getdate()) and day(MemBirthday)-day(getdate())=0 ", new object[0]);
                }
            }
            if ((strMemCustom != "") && (strCustomField != ""))
            {
                strSql.AppendFormat(" and {0} like '%{1}%'", strMemCustom, strCustomField);
            }
            if (strMemState != "")
            {
                strSql.AppendFormat(" and MemState={0} ", int.Parse(strMemState));
            }
            if (strMemUserID != "")
            {
                strSql.AppendFormat(" and MemUserID={0} ", int.Parse(strMemUserID));
            }
            if (strMemStartTime != "")
            {
                strSql.AppendFormat(" and MemCreateTime>='{0}' ", strMemStartTime);
            }
            if (strMemEndTime != "")
            {
                strMemEndTime = Convert.ToDateTime(strMemEndTime).AddDays(1.0).ToString();
                strSql.AppendFormat(" and MemCreateTime<'{0}' ", strMemEndTime);
            }
            if (int.Parse(strPoint) != 0)
            {
                strSql.AppendFormat(" and MemPoint" + strPointSymbols + "{0} ", int.Parse(strPoint));
            }
            if (decimal.Parse(strMemMoney) != 0M)
            {
                strSql.AppendFormat(" and MemMoney" + strMoneySymbols + "{0} ", decimal.Parse(strMemMoney));
            }
            if (decimal.Parse(strConsumeMoney) != 0M)
            {
                strSql.AppendFormat(" and MemConsumeMoney" + strConsumeMoneySymbols + "{0} ", decimal.Parse(strConsumeMoney));
            }
            if (strProvince != "")
            {
                strSql.AppendFormat(" and MemProvince='{0}'", strProvince);
            }
            if ((strCity != "") || (strProvince != ""))
            {
                this.sltCity.Items.Clear();
                PubFunction.BindSysArea(this.sltCity, int.Parse(strProvince));
                this.sltCity.Value = strCity;
                if (strCity != "")
                {
                    strSql.AppendFormat(" and MemCity='{0}'", strCity);
                }
            }
            if ((strCounty != "") || (strCity != ""))
            {
                this.sltCounty.Items.Clear();
                PubFunction.BindSysArea(this.sltCounty, int.Parse(strCity));
                this.sltCounty.Value = strCounty;
                if (strCounty != "")
                {
                    strSql.AppendFormat(" and MemCounty='{0}'", strCounty);
                }
            }
            if ((strVillage != "") || (strCounty != ""))
            {
                this.sltVillage.Items.Clear();
                PubFunction.BindSysArea(this.sltVillage, int.Parse(strCounty));
                this.sltVillage.Value = strVillage;
                if (strVillage != "")
                {
                    strSql.AppendFormat(" and MemVillage='{0}'", strVillage);
                }
            }
            if (strRecommend != "")
            {
                Chain.Model.Mem memModel = new Chain.BLL.Mem().GetModelByMemCard(strRecommend);
                if (memModel != null)
                {
                    strSql.AppendFormat(" and MemRecommendID=" + memModel.MemID, new object[0]);
                }
                else
                {
                    strSql.Append(" and 1=2 ");
                }
            }
            this.BindCustomSelect(this.sltCustomField, strMemCustom, strCustomField, base._UserShopID);
            return strSql.ToString();
        }

        private void SetQuickBtn()
        {
            DataTable dt = PubFunction.GetGroupAuthority(base._UserGroupID);
            this.btnMemRecharge.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemRecharge.Attributes["cls"]));
            this.btnMemChangeCard.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemChangeCard.Attributes["cls"]));
            this.btnMemDelay.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemDelay.Attributes["cls"]));
            this.btnMemLock.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemLock.Attributes["cls"]));
            this.btnMemChangePwd.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemChangePwd.Attributes["cls"]));
            this.btnMemDrawMoney.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnMemDrawMoney.Attributes["cls"]));
            this.btnSenndSMS.Visible = PubFunction.GetPageVisit(dt, int.Parse(this.btnSenndSMS.Attributes["cls"]));
            if (this.btnMemChangePwd.Visible)
            {
                this.btnMemChangePwd.Visible = PubFunction.curParameter.bolPwd;
            }
            if (this.btnMemDelay.Visible)
            {
                this.btnMemDelay.Visible = PubFunction.curParameter.bolPastTime;
            }
            if (this.btnSenndSMS.Visible)
            {
                this.btnSenndSMS.Visible = PubFunction.curParameter.bolSms;
            }
        }
    }


